package mn.idax.exchange.otc.web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mn.idax.exchange.common.SpringUtils;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.constant.RequestUrlConstant;
import mn.idax.exchange.otc.entity.OTCPayModeDetail;
import mn.idax.exchange.otc.enums.OTCBeneficiarysEnum;
import mn.idax.exchange.otc.service.OTCPayModeDetailService;
import mn.idax.exchange.otc.service.impl.MemoryService;
import mn.idax.exchange.otc.util.JsonUtil;
import mn.idax.exchange.otc.util.OTCPayTypeNationUtils;
import mn.idax.exchange.otc.vo.PayMode;
import mn.idax.exchange.otc.vo.PayType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Create by jack_huang on 2018/12/4
 */
@Api(value = "国家-法币-支付方式", tags = {"国家-法币-支付方式"})
@RestController
@RequestMapping(RequestUrlConstant.PAYMODEDETAIL)
public class PayModeDetailController {

    @Autowired
    private OTCPayModeDetailService otcPayModeDetailService;

    @PostMapping(RequestUrlConstant.GET_PAYMODE)
    @ApiOperation(value = "获取国家-法币-支付方式关系", notes = "获取国家-法币-支付方式关系（JSON），(jack_huang，自测通过)")
    public ResponseData getPayMode() throws Exception {

        /**
         * 1 usd 123
         * 1 unit:usd [123]
         */
//        Map<Integer, List<PayType>> map = Maps.newHashMap();
        //支持的所有国家-法币-支付方式
        List<OTCPayModeDetail> enablePayModeDetail = otcPayModeDetailService.getEnablePayModeDetail();
        return ResponseData.success(payModeDetailFormat(enablePayModeDetail));
    }

    /**
     * 格式化数据
     *
     * @param payModeDetail
     * @return
     */
    public List<PayMode> payModeDetailFormat(List<OTCPayModeDetail> payModeDetail) throws IOException {

        // 一转格式{"国家":[{currency:"法币",paymethod:["支付方式","支付方式"]},..],..}
        Map<Integer, List<Map<String, Object>>> contryMap = Maps.newHashMap();
        for (OTCPayModeDetail otcPayModeDetail : payModeDetail) {
            Integer countryid = otcPayModeDetail.getCountryid();
            if (!contryMap.containsKey(countryid)) {
                contryMap.put(countryid, Lists.newArrayList());
            }
            List<Map<String, Object>> currencyList = contryMap.get(countryid);
            Map<String, Object> payModeMap = Maps.newHashMap();
            payModeMap.put("unit", otcPayModeDetail.getCurrency());
            payModeMap.put("payType", otcPayModeDetail.getPaymethod().split(","));
            payModeMap.put("countryNation", SpringUtils.getMessage("message." + OTCBeneficiarysEnum.CountryEnum.getCountry(countryid)));
            payModeMap.put("payTypeNation", OTCPayTypeNationUtils.getPayType(otcPayModeDetail.getPaymethod()));
            payModeMap.put("payTypeIcon", OTCPayTypeNationUtils.getPayTypeIcon(otcPayModeDetail.getPaymethod()));
//            payModeMap.put("floatingRate", );
//            payModeMap.put("fixedSwitch", MemoryService.FIXEDSWITCH.get(countryid));
            currencyList.add(payModeMap);
            contryMap.put(countryid, currencyList);
        }

        // 二转格式[[country:"国家",payType:[{unit:"法币"，payType:["支付方式","支付方式"]},..]],..]
        List<PayMode> payModesList = Lists.newArrayList();
        for (Integer countryId : contryMap.keySet()) {
            List<PayType> payTypes = JsonUtil.readValue(JsonUtil.writeValue(contryMap.get(countryId)), new TypeReference<List<PayType>>() {
            });
            payModesList.add(PayMode.builder()
                    .country(countryId)
                    .payType(payTypes)
                    .floatingRate((BigDecimal)(MemoryService.FLOATINGRATE.get(countryId)))
                    .fixedSwitch((boolean)MemoryService.FIXEDSWITCH.get(countryId))
                    .countryNation(SpringUtils.getMessage("message." + OTCBeneficiarysEnum.CountryEnum.getCountry(countryId)))
                    .build());
        }
        return payModesList;
    }
}
